package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthorDto;
import com.heima.user.dto.UserDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Override
    public ResponseResult listByStatus(UserDto dto) {
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
//        判断status是否为空
        if (dto.getStatus() != null){
//            不为空 查询
            query.eq(ApUserRealname::getStatus,dto.getStatus());
        }
//        为空查询全部
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());

        IPage<ApUserRealname> page1 = this.page(page, query);

        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
//        返回结果
        return result;
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthorDto dto, int type) {
//        查询app认证是名表
        ApUserRealname apUserRealname = this.getById(dto.getId());
//        如果审核失败
        if (type == 0){
//            修改失败状态 ，添加失败原因
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
            return ResponseResult.okResult();
        }
//        获取user
        LambdaQueryWrapper<ApUser> query = new LambdaQueryWrapper<>();
        query.eq(ApUser::getId,apUserRealname.getUserId());
        ApUser apUser = apUserMapper.selectOne(query);
//          审核通过
//        注册自媒体账号
        WmUser wmUser = createWmUser(apUser);
//        注册 并修改 作者账户
        ApAuthor apAuthor = createArticle(wmUser,apUser);
//        判断账户是否为空
        if (apAuthor == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        if (wmUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
//        如果不是空继续修改自媒体账号
        wmUser.setApAuthorId(apAuthor.getId());
        ResponseResult<WmUser> wmUserResponseResult = mediaClient.update(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
//        修改为用户认证表
           apUserRealname.setStatus(9);
           this.updateById(apUserRealname);
//           修改用户注册表的flag为1
            apUser.setFlag(1);
            apUserMapper.updateById(apUser);

        }


        return ResponseResult.okResult();


    }
    @Autowired
    private MediaClient mediaClient;
    @Autowired
    private ArticleClient articleClient;

    /**
     * 创建作者账户
     * @param wmUser
     * @param apUser
     * @return
     */
    private ApAuthor createArticle(WmUser wmUser, ApUser apUser) {
        ApAuthor apAuthor = new ApAuthor();
        BeanUtils.copyProperties(apUser,apAuthor);
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> apAuthorResponseResult = articleClient.saveAuthor(apAuthor);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor data = apAuthorResponseResult.getData();
            return data;
        }
        return null;
    }

    /**
     * 创建自媒体账号
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
//        添加自媒体账户
        ResponseResult<WmUser> wmUserResponseResult = mediaClient.saveUser(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser data = wmUserResponseResult.getData();
            return data;
        }
        return null;
    }


}
